package controllers.dbWrapper;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Scanner;

import controllers.ExceptionsTypes;

//import com.mysql.jdbc.Driver;

public class DBWrapper {

	public static Connection getConnection() throws Exception{
		try{
			if(connection == null || !connection.isValid(0)){
				Connection c = null;
			    Properties connectionProps = new Properties();
			    ;
			    connectionProps.put("user", getUsername());
			    connectionProps.put("password", getPassword());
			    if (getDbms().equals("mysql")) {
			    	c = DriverManager.getConnection("jdbc:" + getDbms() + "://" + getServername() +":" + getPortnumber() + "/", connectionProps);
			    } else if (getDbms().equals("derby")) {
			    	c = DriverManager.getConnection("jdbc:" + getDbms() + ":" + getDbname() + ";create=true", connectionProps);
			    }
			    connection = c;
			}
		}
		catch (SQLException e) {
			throw new Exception(ExceptionsTypes.CONNECTION_ERROR.name());
		}
		return connection;
	}

	public static String getUsername() {
		if(userName == null){
			try {
				readFromConfigurationFile();
			} catch (Exception e) {
				if(e.getMessage().equalsIgnoreCase(ExceptionsTypes.MISSING_CONFIGURATION_FILE.name()));
				
			}
		}
		return userName;
	}

	public static String getPassword() {
		if(password == null){
			try {
				readFromConfigurationFile();
			} catch (Exception e) {
				if(e.getMessage().equalsIgnoreCase(ExceptionsTypes.MISSING_CONFIGURATION_FILE.name()));
				
			}
		}
		return password;
	}

	public static String getDbms() {
		if(dbms == null){
			try {
				readFromConfigurationFile();
			} catch (Exception e) {
				if(e.getMessage().equalsIgnoreCase(ExceptionsTypes.MISSING_CONFIGURATION_FILE.name()));
			}
		}
		return dbms;
	}

	public static String getServername() {
		if(serverName == null){
			try {
				readFromConfigurationFile();
			} catch (Exception e) {
				if(e.getMessage().equalsIgnoreCase(ExceptionsTypes.MISSING_CONFIGURATION_FILE.name()));
			}
		}
		return serverName;
	}

	public static String getPortnumber() {
		if(portNumber == null){
			try {
				readFromConfigurationFile();
			} catch (Exception e) {
				if(e.getMessage().equalsIgnoreCase(ExceptionsTypes.MISSING_CONFIGURATION_FILE.name()));
			}
		}
		return portNumber;
	}

	public static String getDbname() {
		if(dbName == null){
			try {
				readFromConfigurationFile();
			} catch (Exception e) {
				if(e.getMessage().equalsIgnoreCase(ExceptionsTypes.MISSING_CONFIGURATION_FILE.name()));
			}
		}
		return dbName;
	}
	
	@SuppressWarnings("resource")
	public static void readFromConfigurationFile() throws Exception{
		if(configurationFile == null || !configurationFile.exists()){
			configurationFile.createNewFile();
			throw new Exception(ExceptionsTypes.MISSING_CONFIGURATION_FILE.name());
		}
		Scanner sc = new Scanner(new FileInputStream(configurationFile));
		if(!sc.hasNextLine()) throw new Exception(ExceptionsTypes.CORRUPT_CONFIGURATION_FILE.name());
		dbms = sc.nextLine();
		if(!sc.hasNextLine()) throw new Exception(ExceptionsTypes.CORRUPT_CONFIGURATION_FILE.name());
		serverName = sc.nextLine();
		if(!sc.hasNextLine()) throw new Exception(ExceptionsTypes.CORRUPT_CONFIGURATION_FILE.name());
		portNumber = sc.nextLine();
		if(!sc.hasNextLine()) throw new Exception(ExceptionsTypes.CORRUPT_CONFIGURATION_FILE.name());
		userName = sc.nextLine();
		if(!sc.hasNextLine()) throw new Exception(ExceptionsTypes.CORRUPT_CONFIGURATION_FILE.name());
		password = sc.nextLine();
		if(!sc.hasNextLine()) throw new Exception(ExceptionsTypes.CORRUPT_CONFIGURATION_FILE.name());
		dbName = sc.nextLine();
	}

	public static File getConfigurationFile() {
		return configurationFile;
	}

	public static void setConfigurationFile(File configurationFile) {
		DBWrapper.configurationFile = configurationFile;
	}


	private static String userName = null;
	private static String password = null;
	private static String dbms = null;
	private static String serverName = null;
	private static String portNumber = null;
	private static String dbName = null;
	private static Connection connection = null;
	private static File configurationFile = new File("../unisannio-caterina-controller/connectionConfig");
}
